<template>
	<view class="container">
		<view class="header">
			<button class="back-button" @click="goBack">返回</button>
			<!-- <text class="title">工单详情</text> -->
		</view>
		<view class="content">
			<view class="status">
				<view class="text-container">
					<text class="status-text">工单状态：{{ taskstatusMap[task.taskStatus] }}
					</text>
					<text class="order-number">工单号：{{ task.taskCode }}</text>
				</view>
				<image class="detail-img" src="/static/img/task_detail.jpg" mode="cover"></image>
			</view>
		</view>
		<view class="info_content">
			<view class="info">
				<text>设备号：{{ task.innerCode }}</text>
				<text>创建时间：{{ task.createTime }}</text>
				<text>地址：{{ task.addr }}</text>
				<text>工单类型：{{ taskTypeMap[task.productTypeId] }}</text>
				<text>备注：{{ task.desc }}</text>
			</view>
		</view>
		<!-- 接受工单按钮 -->
		<view class="footer" v-if="task.taskStatus === 1">
			<button class="reject-button" @click="rejectTask">拒绝</button>
			<button class="accept-button" @click="acceptTask">接受</button>
		</view>
		<!-- 完成工单按钮 -->
		<view class="footer" v-if="task.taskStatus === 2">
			<button class="reject-button" @click="rejectTask">取消</button>
			<button class="accept-button" @click="completeTask">完成</button>
		</view>


		<!-- 弹框 -->
		<view v-if="showRejectDialog" class="reject-dialog">
			<view class="reject-header">
				<text>拒绝原因</text>
			</view>
			<textarea class="reject-textarea" placeholder="请输入你对此工单的拒绝原因" v-model="rejectReason"></textarea>
			<view class="reject-footer">
				<button class="cancel-button" @click="cancelReject">取消</button>
				<button class="submit-button" @click="submitReject">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
import {
	getByTaskId,
	acceptTask,
	cancelTask
} from '@/utils/api'; // 导入api.js中的方法
import { completeTask } from '../../utils/api';
export default {
	data() {
		return {
			task: {},
			taskTypeMap: {
				1: '投放工单',
				2: '维修工单',
				3: '检查工单'
			},
			showRejectDialog: false,
			rejectReason: '',
			taskstatusMap: {
				1: '待办',
				2: '进行',
				3: '取消',
				4: '完成'
			}
		};
	},
	onLoad(options) {
		if (options.taskId) {
			console.log("初始值", options.taskId)
			const taskId = parseInt(options.taskId);
			console.log("接受的参数是", taskId)
			this.fetchTaskDetail(taskId);
		}
	},
	methods: {

		async fetchTaskDetail(taskId) {
			// 根据 taskId 获取工单详情
			console.log('taskId:', taskId);
			// 这里可以调用 API 获取工单详情数据
			try {
				const data = await getByTaskId(taskId)
				console.log("工单详情数据是", data)
				this.task = data;
			} catch (error) {
				console.error('获取工单详情失败', error);
			}

		},

		goBack() {
			uni.navigateBack();
		},
		async acceptTask() {
			// 接受工单的逻辑
			console.log('接受工单');
			try {
				await acceptTask(this.task.taskId)
				uni.showToast({
					title: '操作成功',
					icon: 'success',
					duration: 2000
				});
				//关闭弹框，
				// 设置一个2秒的延迟
				setTimeout(() => {
					this.showRejectDialog = false;
				}, 2000); // 2秒后关闭弹框
				// 跳转进行工单table
				uni.switchTab({
					url: '/pages/tasking/tasking'
				})
			} catch (error) {
				console.error('获取工单信息失败', error);
			}
		},
		async completeTask() {
			// 完成工单的逻辑
			console.log('完成工单');
			try {
				await completeTask(this.task.taskId)
				//操作成功弹框
				uni.showToast({
					title: '操作成功',
					icon: 'success',
					duration: 2000
				});
				// 设置一个2秒的延迟
				setTimeout(() => {
					this.showRejectDialog = false;
				}, 2000); // 2秒后关闭弹框


			} catch (error) {
				console.error('完成工单信息失败', error);
			}
		},
		rejectTask() {
			// 拒绝工单的逻辑
			console.log("点击拒绝")
			this.showRejectDialog = true;
		},
		cancelReject() {
			console.log("点击取消")
			this.showRejectDialog = false;
			this.rejectReason = "";
		},
		async submitReject() {
			// 提交拒绝理由到服务器
			console.log('提交拒绝理由：', this.rejectReason);
			try {
				await cancelTask(this.task.taskId, {
					desc: this.rejectReason
				})
			} catch (error) {
				console.error('获取工单信息失败', error);
			}
		}
	}
};
</script>

<style>
.container {
	background-color: #f5f5f5;
	height: 100px;
}

.header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	background-color: #333;
	color: #fff;
	padding: 10px;
	height: 30px;
}

.back-button {
	background: none;
	border: none;
	color: #fff;
	font-size: 16px;
	margin-left: 0px;
}

.title {
	font-size: 20px;
	font-weight: bold;
	margin-left: auto;
	margin-right: 150px;
}

.content {
	background-color: #fff;
}

.status {
	display: flex;
	align-items: center;
	justify-content: space-between;
	background-color: #333;
	color: #fff;
	padding: 10px;
	border-radius: 5px;
}

.text-container {
	display: flex;
	flex-direction: column;
	align-items: flex-start;
	margin-left: 20px;
}

.status-text {
	font-size: 16px;
	font-weight: bold;
	margin-bottom: 10px;
}

.order-number {
	font-size: 14px;
	font-weight: bold;
}

.detail-img {
	height: 110px;
	width: 110px;
	margin-left: 20px;
}

.info_content {
	height: 450px;
}

.info text {
	display: block;
	margin-top: 20px;
	margin-bottom: 20px;
	margin-left: 20px;
	font-size: 14px;
	padding: auto;
	color: #333;
}

.footer {
	display: flex;
	justify-content: space-between;
	margin-top: 20px;
}

.reject-button,
.accept-button {
	flex: 1;
	margin: 0 5px;
	padding: 10px;
	border-radius: 5px;
	color: #fff;
	font-size: 16px;
	margin-bottom: 10px;
}

.reject-button {
	background-color: #ff4d4f;
}

.accept-button {
	background-color: #1aad19;
}

.reject-dialog {
	position: fixed;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	width: 300px;
	background-color: #fff;
	border-radius: 5px;
	box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 20px;
}

.reject-header {
	font-size: 18px;
	font-weight: bold;
	margin-bottom: 10px;
}

.reject-textarea {
	width: 100%;
	height: 60px;
	margin-bottom: 20px;
	padding: 10px;
	border: 1px solid #ccc;
	border-radius: 5px;
}

.reject-footer {
	display: flex;
	justify-content: space-between;
	width: 100%;
}

.cancel-button,
.submit-button {
	flex: 1;
	margin: 0 5px;
	padding: 10px;
	border-radius: 5px;
	color: #fff;
	font-size: 16px;
}

.cancel-button {
	background-color: #e7696b;
}

.submit-button {
	background-color: #467246;
}
</style>